package com.boot.project.wechat.applet.web;

import com.boot.project.domain.OrderDo;
import com.boot.project.service.OrderService;
import com.boot.project.util.IdGen;
import com.boot.project.util.Render;
import com.boot.project.wechat.applet.dto.RechargeDto;
import com.boot.project.wechat.applet.service.PayService;
import com.boot.project.wechat.applet.util.AppletPayUtil;
import com.boot.project.wechat.base.BjddController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;


/****************************************************
 *
 *
 *
 * @author hw
 * @version 1.0
 **************************************************/

@RestController
@RequestMapping("/m/recharge")
public class RechargeAPI extends BjddController {

    @Autowired
    private PayService payService;

    @Autowired
    private OrderService orderService;

    //@ApiOperation(value = "创建充值订单", notes = "创建充值订单")
    @PostMapping
    public Object order(@RequestBody RechargeDto rechargeDto) throws Exception {
        /**
         微信小程序支付
         */
        //获取code
        String code = rechargeDto.getCode();
        //调用接口获取openId
        String openId = AppletPayUtil.getOpenIdByCode(code);
        /*
            生成订单....，这里只创建了订单号
         */
        //订单号  uuid
        String outTradeNo= IdGen.uuid();

        int  orderType = rechargeDto.getOrderType();

        /**
         * 生成相应的订单 1。订购场地 2.充值 3.预付款
         */
        OrderDo orderDo = new OrderDo();
        orderDo.setOrderId(outTradeNo);
        orderDo.setCash(rechargeDto.getRechargeMoney()/100);
        orderDo.setCommodityId(orderType);
        if (orderType == 1){
            orderDo.setCourtId(rechargeDto.getCourtId());
            orderDo.setCourtTimeId(rechargeDto.getCourtTimeId());
        }
        orderDo.setOpenId(openId);
        Date date = new Date();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        orderDo.setStartTime(date);
        orderDo.setStatus(0);
        orderService.add(orderDo);
        return Render.ok(payService.unifiedOrder(outTradeNo,rechargeDto.getRechargeMoney(),openId,orderType));
    }
}
